import {request} from "../utils/request/activity";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activities:[],
    activityList:[],
    activeItem:'全部'
    
  },
  toActivityList(e){
    console.log(e.currentTarget.dataset.name);
    //将活动名称存储至本地
    wx.setStorageSync('activityName', e.currentTarget.dataset.name)
    wx.setStorageSync('activeItem', this.data.activeItem)
    wx.navigateTo({
      url: '../activityList/activityList',
    })
  },
  //搜索
  bindSearchConfirm(e){
    let that=this;
    console.log(e.detail.value);
    console.log(this.data.activeItem);
    if(this.data.activeItem=='全部'){
      this.setData({
        activeItem:''
      })
    }
    request({
      url: '/getActivityTitle',
      data:{
        title:e.detail.value,
        communityName:this.data.activeItem
      },
    }).then(result=>{
      console.log(result);
      if(that.data.activeItem=='')
      that.setData({
        activeItem:'全部'
      })
      that.setData({
            activityList:result
          })
    })
    


  },
  QueryParams:{
    pagenum:1,
    pagesize:4,
    communityName:'',
  },
  //总页数
  totalPage:1,

  getName(e){
    console.log(e);
    this.setData({
      activityList:[]
    })
    this.setData({
      activeItem:e.target.dataset.num
    })
    // this.setData({
    //   communityName:e.target.dataset.num
    // })
    this.QueryParams.communityName=e.target.dataset.num;
    this.activityList();
    
  },
  allName(e){
    let that=this;
    that.setData({
      activeItem:e.target.dataset.num
    })
    request({
      url: '/getActivity',
    }).then(result=>{
      console.log(result);
      that.setData({
            activityList:result
          })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setStorageSync('activeItem', this.data.activeItem)
    let that=this;
    wx.request({
      url: 'http://localhost/activity/getActivityAllName',
      success:function(result){
        console.log(result.data);
        that.setData({
          activities:result.data
        })
      }
    });
    request({
      url: '/getActivity',
    }).then(result=>{
      console.log(result);
      that.setData({
            activityList:result
          })
    })



    //请求数据
      this.activityList();
    },
    async activityList(){
      //异步中实现同步
      const res = await request({url:"/getActivity/page",data:this.QueryParams});
      console.log("4444")
      console.log(this.QueryParams);
      console.log(res);
      //获取总条数
       const total = res.total;
       console.log(total);
     //  //计算总页数
      this.totalPage = Math.ceil(total/this.QueryParams.pagesize);
      this.setData({
        activityList:[...this.data.activityList,...res.list]
       })
       
  },

  //页面上滑 滚动条触底事件
 onReachBottom(){
  //1：判断还有没有下一页数据  当前的页码是否大于等于总页数
  if(this.QueryParams.pagenum>=this.totalPage){
    //没有下一页数据
    wx.showToast({
      title: '到底了',
    })
  }else{
    //还有下一页数据
    this.QueryParams.pagenum++;
    this.activityList();
  }

},
onPullDownRefresh:function(){
  console.log("hhhhhhh");
  //1.重置数组
    this.setData({
      activityList:[]
    })
    //2.重置页码
    this.QueryParams.pagenum=1;
    //3.发送请求
    this.activityList();
}
})